package com.tomorrow.runner.entertainment.run;

import android.os.Bundle;

import com.amap.api.maps2d.model.LatLng;
import com.tomorrow.runner.R;
import com.tomorrow.runner.activity.basic.BasicActivity;
import com.tomorrow.runner.entertainment.Buff;
import com.tomorrow.runner.entertainment.Effect;
import com.tomorrow.runner.entertainment.Player;
import com.tomorrow.runner.util.LatLngUtil;

import cn.sucun.android.log.Log;

/**
 *
 * Created by tomorrow on 16-4-30.
 */
public class Tracker extends Player {

    private int icon;
    private Player target;
    private double speed;
    private Effect effect;
    private String message;
    private Buff buff;
    private Thread updateThread;
    private Bundle mOldStatus = new Bundle();
    public Tracker(int id, long lifeTime, LatLng position, BasicActivity context){
        super(id,position,lifeTime,context);
        this.icon = R.mipmap.zombie_red;
        this.status = STATUS_WAIT;
        this.target = null;
        this.speed = 0.0;
        this.effect = null;
        this.buff = null;
        this.message = "";
        effect = new TrackerEffect();
    }
    public synchronized void setTarget(Player target) {
        if (target == null){
            changeStatus(STATUS_WAIT);
            return;
        }
        if (target.equals(this)){
            return;
        }
        this.target = target;

    }

    public void changeStatus(int status){
        if (this.status == status){
            return;
        }
        switch (status){
            case STATUS_ACTIVE:
                active();
                break;
            case STATUS_WAIT:
                waiting();
                break;
            case STATUS_DONE:
                done();
                break;
            case STATUS_DEATH:
                death();
                break;
        }
        super.changeStatus(status);
    }

    private void death() {
        health = 0;
        speed = 0;
        target = null;
        this.effect = null;
        this.buff = null;
        this.icon = R.mipmap.zombie_gray;
        this.message = "被吃掉了";
    }

    private void done() {
        contact(target);
        context.playSound(R.raw.eat);
    }

    private void waiting() {
        saveOldStatus();
    }

    private void saveOldStatus() {
        mOldStatus.putDouble("speed",speed);
    }

    private void loadOldStatus(){
        speed = mOldStatus.getDouble("speed",speed);
    }


    private void active() {
        if (updateThread == null || !updateThread.isAlive()){
            updateThread = new UpdateThread();
        }
        updateThread.start();
    }

    public void contact(Player player){
        effect.contact(this,player);
    }
    public void setHealth(int health) {
        super.setHealth(health);
        if (health <= 0){
            changeStatus(STATUS_DEATH);
        }
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Buff getBuff() {
        return buff;
    }

    public void setBuff(Buff buff) {
        this.buff = buff;
    }
    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }
    public class UpdateThread extends Thread{
        @Override
        public void run() {
            super.run();
            loadOldStatus();
            while (status == STATUS_ACTIVE){
                if (target != null){
                    //计算距离
                    double distance = LatLngUtil.distanceBetween(position,target.getPosition());
                    Log.i("Tracker","(distance="+distance);
                    if (((int)distance) == 0){
                        message = target.getId() + "号被"+id+"号吃掉了!";
                        changeStatus(STATUS_DONE);
                        return;
                    }
                    else{
                        message = String.format("距离目标%d米",(int)distance);
                    }
                    //计算角度
                    double difflat = target.getPosition().latitude - position.latitude;
                    double difflng = target.getPosition().longitude - position.longitude;
                    double total = Math.sqrt(difflat * difflat + difflng * difflng);
                    Log.i("Tracker","(difflat / total)="+(difflat / total));
                    Log.i("Tracker","(difflng / total)="+(difflng / total));
                    //计算分量
                    double m = speed * (total / distance);
                    double lat = m * (difflat / total);
                    double lng = m * (difflng / total);
                    Log.i("Tracker","latM="+lat);
                    Log.i("Tracker","lngM="+lng);
                    //获取偏移位置坐标
                    position = new LatLng(position.latitude+lat,position.longitude+lng);
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        changeStatus(STATUS_DEATH);
    }
}
